package com.fingard.dsp.bank.directbank.yeepay02;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.yeepay.g3.sdk.yop.client.YopRequest;
import com.yeepay.g3.sdk.yop.client.YopResponse;
import com.yeepay.g3.sdk.yop.client.YopRsaClient;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class YEEPAY02Balance extends YEEPAY02Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        RetAtsBalDetail retDetail = new RetAtsBalDetail();
        retAts.writeStart();
        try {
            while (p_req7506.readRecord()) {
                ActSetItem actItem = getBankConfig().getActSetItem(p_req7506.readedDetail.actNum, "");
                if (actItem == null) {
                    retDetail.respCode = "-1";
                    if (actItem == null) {
                        retDetail.respInfo = "未找到账号" + p_req7506.readedDetail.actNum + "的批量商户配置节点";
                    }
                    retAts.writeRetDetail(retDetail, p_req7506.isBatch);
                    return retAts;
                }

                Map<String, String> params = new HashMap<String, String>();
                params.put("appKey", actItem.merchantId);
                Calendar ts = Calendar.getInstance();
                ts.setTime(new Date());
                String transTime = String.valueOf(ts.getTimeInMillis());
                params.put("ts", transTime);

                String url = bankFront.getUrl("balance");
                WriteBankLogLn("提交银行的地址后缀：" + url);
                YopRequest yoprequest = getYoprequest(params, actItem);
                WriteBankLogStep2(yoprequest.toQueryString());

                //向YOP发请求
                YopResponse tmpresponse = YopRsaClient.post(url, yoprequest);
                WriteBankLogStep3(tmpresponse.toString());
                if (tmpresponse != null) {
                    String status = tmpresponse.getState();
                    Map map = parseResponse(tmpresponse.getStringResult());
                    if ("SUCCESS".equals(status)) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = status;
                        retDetail.availBal = map.get("accountAmount").toString();
                        retDetail.actNum = map.get("customerNumber").toString();
                        retDetail.yesterdayBal = map.get("rjtValidAmount").toString();
                        retDetail.cashPoolAvailBal = map.get("wtjsValidAmount").toString();
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpresponse.getError().getMessage();
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpresponse.toString();
                }
                retAts.writeRetDetail(retDetail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
